﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Text;

namespace WindowService
{
    public enum NetworkCardType
    {
        Physical = 0,
        Virtual = 1,
        Wireless = 2,
        Other = 2
    };

    public class Wmi
    {
        public Dictionary<string, string[]> GetAllInfomation()
        {
            string defRKey = @"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}";
            NetworkInterface[] netwis = NetworkInterface.GetAllNetworkInterfaces();
            Dictionary<string,string[]> networks = new Dictionary<string,string[]>();
            try
            {
                foreach (NetworkInterface netwi in netwis)
                {
                    string sip = "";

                    foreach (UnicastIPAddressInformation a in netwi.GetIPProperties().UnicastAddresses)
                    {
                        if (a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            sip += a.Address.ToString();
                        }
                    }
                    string srKey = defRKey + "\\" + netwi.Id + "\\Connection";
                    string sPnp = "";
                    string sSubM = "";

                    if (netwi.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        RegistryHelper registryHelper = new RegistryHelper();
                        sPnp = registryHelper.GetRegistryData(Registry.LocalMachine, srKey, "PnpInstanceID");
                        sSubM = registryHelper.GetRegistryData(Registry.LocalMachine, srKey, "MediaSubType");

                        bool isPa = false;
                        if (sPnp.Length > 3)
                        {
                            isPa = sPnp.IndexOf("PCI") >= 0 && sPnp.IndexOf("SUBSYS_") >= 0;
                        }
                        //
                        string[] network = new string[6];
                        network[0] = (isPa ? NetworkCardType.Physical : (sSubM == "1" ? NetworkCardType.Virtual : (sSubM == "2") ? NetworkCardType.Wireless : NetworkCardType.Other)).ToString();
                        network[1] = netwi.GetPhysicalAddress().ToString();
                        network[2] = netwi.Speed.ToString();
                        network[3] = netwi.OperationalStatus.ToString();
                        network[4] = sip;
                        network[5] = netwi.Name;

                        networks.Add(netwi.Id, network);
                    }
                }
            }
            catch (Exception e)
            {
                Log.WriteLog("Wmi Get wmi infomation error: " + e.Message, LogType.Error);
            }


            return networks;
        }

        public string getMac()
        {
            Dictionary<string, string[]> allInfomation = GetAllInfomation();
            string mac = "FFFFFFFFFFFF";
            foreach (var network in allInfomation)
            {
                if (network.Value[0] == NetworkCardType.Physical.ToString())
                {
                    mac = GetMinMacAddress(network.Value[1], mac);
                }
            }
            return mac;
        }
        public string GetMinMacAddress(string mac1, string mac2)
        {
            string mac1High = mac1.Substring(0, 6);
            string mac2High = mac2.Substring(0, 6);
            long mac1HighLong = Convert.ToInt64(mac1High, 16);
            long mac2HighLong = Convert.ToInt64(mac2High, 16); 

            if (mac1HighLong < mac2HighLong)
            {
                return mac1;
            }
            else if (mac1HighLong > mac2HighLong)
            {
                return mac2;
            }

            string mac1Low = mac1.Substring(6, 6);
            string mac2Low = mac2.Substring(6, 6);
            long mac1LowLong = Convert.ToInt64(mac1Low, 16);
            long mac2LowLong = Convert.ToInt64(mac2Low, 16); 

            if (mac1LowLong < mac2LowLong)
            {
                return mac1;
            }

            return mac2;
        }
    }
}
